<?php

/**
 * Classe Util: è una classe con dei metodi di utilizzo comune richiamabili in modo statico 
 * all'interno dell'applicazione
 * 
 * @author Stefano Poncini
 */
class Util {

    public function __construct() {
        
    }

    /**
     * Metodo convertDateTime: gli viene passata una data e la converte in timestamp
     * 
     * @param string $str
     * @return int
     */
    public static function convertDateTime($str) {

        list($date, $time) = explode(' ', $str);
        list($year, $month, $day) = explode('-', $date);
        list($hour, $minute, $second) = explode(':', $time);

        $timestamp = mktime($hour, $minute, $second, $month, $day, $year);

        return $timestamp;
    }

    /**
     * Metodo getDate: da una data non formattata ne ritorna una formattata correttamente
     * 
     * @param string $date_input
     * @return string
     */
    public static function getDate($date_input) {
        $date = explode(" ", $date_input);
        $date = explode("-", $date[0]);
        $month = Util::getMonth($date[1]);

        return $date[2] . ' ' . $month . ' ' . $date[0];
    }
    
    /**
     * Metodo getTime: ritorna l'ora passandogli una data
     * 
     * @param type $date_input
     * @return string
     */
    public static function getTime($date_input) {
        $time = explode(" ", $date_input);
        $time = explode(":", $time[1]);
        
        return $time[0] . ':' . $time[1];
    }

    /**
     * Metodo getMonth: dato l'indice del mese ritorna la stringa
     * 
     * @param int $month
     * @return string
     */
    public static function getMonth($month) {
        switch ($month) {
            case 1:
                return 'Gennaio';
                break;
            case 2:
                return 'Febbraio';
                break;
            case 3:
                return 'Marzo';
                break;
            case 4:
                return 'Aprile';
                break;
            case 5:
                return 'Maggio';
                break;
            case 6:
                return 'Giugno';
                break;
            case 7:
                return 'Luglio';
                break;
            case 8:
                return 'Agosto';
                break;
            case 9:
                return 'Settembre';
                break;
            case 10:
                return 'Ottobre';
                break;
            case 11:
                return 'Novembre';
                break;
            case 12:
                return 'Dicembre';
                break;
        }
    }

    /**
     * Metodo getYears: ritorna l'età corrente passandogli la data di nascita
     * 
     * @param int $year
     * @param int $month
     * @param int $day
     * @return int
     */
    public static function getYears($year, $month, $day) {
        $currentYear = date("Y", time());
        $currentMonth = date("m", time());
        $currentDay = date("d", time());

        if ($currentMonth < $month) {
            return ($currentYear - $year) - 1;
        } else {
            if ($currentDay >= $day) {
                return ($currentYear - $year);
            }
        }
    }

    /**
     * Metodo generateActivationID: genera un id random di numeri e lettere minuscole e maiuscole
     * 
     * @param ïnt $length
     * @return string
     */
    public static function generateActivationID($length = 10) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $randomString;
    }

    /**
     * Metodo parsePostContent: sostituisce gli asconimi degli smiles con il tag <img> e ritorna la nuova stringa
     * 
     * @static
     * @param string $content
     * @return string
     */
    public static function parsePostContent($content){
        
        $content = str_replace(":angry:", "<img src='static/images/emo/angry.gif' />", $content); 
        $content = str_replace(":biggrin:", "<img src='static/images/emo/biggrin.gif' />", $content); 
        $content = str_replace(":blink:", "<img src='static/images/emo/blink.gif' />", $content); 
        $content = str_replace(":blushing:", "<img src='static/images/emo/blushing.gif' />", $content); 
        $content = str_replace(":bored:", "<img src='static/images/emo/bored.gif' />", $content);
        $content = str_replace(":confused:", "<img src='static/images/emo/confused.gif' />", $content); 
        $content = str_replace(":cool:", "<img src='static/images/emo/cool.gif' />", $content); 
        $content = str_replace(":crying:", "<img src='static/images/emo/crying.gif' />", $content); 
        $content = str_replace(":cursing:", "<img src='static/images/emo/cursing.gif' />", $content); 
        $content = str_replace(":drool:", "<img src='static/images/emo/drool.gif' />", $content); 
        $content = str_replace(":flapper:", "<img src='static/images/emo/flapper.gif' />", $content); 
        $content = str_replace(":glare:", "<img src='static/images/emo/glare.gif' />", $content); 
        $content = str_replace(":huh:", "<img src='static/images/emo/huh.gif' />", $content); 
        $content = str_replace(":laugh:", "<img src='static/images/emo/laugh.gif' />", $content); 
        $content = str_replace(":lol:", "<img src='static/images/emo/lol.gif' />", $content); 
        $content = str_replace(":love:", "<img src='static/images/emo/love.gif' />", $content); 
        $content = str_replace(":mad:", "<img src='static/images/emo/mad.gif' />", $content); 
        $content = str_replace(":mellow:", "<img src='static/images/emo/mellow.gif' />", $content); 
        $content = str_replace(":ohmy:", "<img src='static/images/emo/ohmy.gif' />", $content); 
        $content = str_replace(":rolleyes:", "<img src='static/images/emo/rolleyes.gif' />", $content); 
        $content = str_replace(":sad:", "<img src='static/images/emo/sad.gif' />", $content); 
        $content = str_replace(":scared:", "<img src='static/images/emo/scared.gif' />", $content); 
        $content = str_replace(":sleep:", "<img src='static/images/emo/sleep.gif' />", $content); 
        $content = str_replace(":smile:", "<img src='static/images/emo/smile.gif' />", $content); 
        $content = str_replace(":sneaky:", "<img src='static/images/emo/sneaky.gif' />", $content); 
        $content = str_replace(":thumbdn:", "<img src='static/images/emo/thumbdn.gif' />", $content); 
        $content = str_replace(":thumbsup:", "<img src='static/images/emo/thumbsup.gif' />", $content); 
        $content = str_replace(":thumbup:", "<img src='static/images/emo/thumbup.gif' />", $content); 
        $content = str_replace(":tongue:", "<img src='static/images/emo/tongue.gif' />", $content); 
        $content = str_replace(":unsure:", "<img src='static/images/emo/unsure.gif' />", $content); 
        $content = str_replace(":w00t:", "<img src='static/images/emo/w00t.gif' />", $content); 
        $content = str_replace(":wink:", "<img src='static/images/emo/wink.gif' />", $content); 
        
        /*
        if(preg_match_all('#\[quote=(.*?)\](.*?)#is', $str, $matches)==preg_match_all('#\[/quote\]#is', $str, $matches)) {
            array_push($format_search, '#\[quote=(.*?)\](.*?)#is');
            array_push($format_search, '#\[/quote\]#is');

            array_push($format_replace, '<div class="messageQuoted"><a class="lblackb" href="index.php?explore=userview&userv=$1">$1</a> wrote :<br />$2');
            array_push($format_replace, '</div>');
        }

        $str=preg_replace($format_search, $format_replace, $str);
        */
        
        //$content = Util::quote($content);
        
        
        $format_search = array();
        $format_replace = array();
        if(preg_match_all('#\[quote=(.*?)\](.*?)#is', $content)==preg_match_all('#\[/quote\]#is', $content)) {
            array_push($format_search, '#\[quote=(.*?)\](.*?)#is');
            array_push($format_search, '#\[/quote\]#is');

            array_push($format_replace, '<div class="prequote"><b>$1 ha scritto:</b><div class="quote_odd">$2');
            array_push($format_replace, '</div></div>');
            
        }
        
        
        
        
        $content=preg_replace($format_search, $format_replace, $content);
        
  
        //$content=preg_replace_callback('#\[quote=(.*?)\](.*?)\[/quote\]#is', '<div class="prequote"><b>$1 ha scritto:</b><div class="quote">$2</div></div>', $content);
        
        //$content = htmlspecialchars($content, ENT_QUOTES); 
        $content = str_replace("&lt;br /&gt;", "<br />", $content); 
        
        return $content;
        
     
        
        
    }
    
    
    /**
     * Metodo parseToDB: prepara la stringa da mettere sul database
     * @param string $txt
     * @return string
     * @static
     */
    public static function parseToDB($txt){
        
        $txt = str_replace("\n",'<br />',$txt);
        $txt = str_replace("\r",'<br />',$txt);
        $txt = utf8_encode($txt);
        
        return $txt;
    }
    
}

?>